function int = acrudisc(sd, md, rv, disc, per, basis)
%ACRUDISC Accrued interest of discount security paying at maturity.
%   ACRUDISC returns the accrued interest of a discount security paid at
%   maturity.
%
%   INT = acrudisc(SD, MD, RV, DISC)
%   INT = acrudisc(SD, MD, RV, DISC, PER, BASIS)
%
%   Optional Inputs: PER, BASIS
%
%   Inputs:
%        SD - Scalar or vector of settlement dates as serial date numbers
%             or date strings.
%
%        MD - Scalar or vector of maturity dates serial date numbers or
%             date strings.
%
%        RV - Scalar or vector of par values for the security serial date
%             numbers or date strings.
%
%      DISC - Scalar or vector of discount rates for the security.
%
%   Optional Inputs:
%       PER - Scalar or vector of the number of coupon periods per year
%             (default = 2).
%
%     BASIS - Scalar or vector of the day-count basis.
%             Valid basis include:
%                0 - actual/actual (default)
%                1 - 30/360     SIA
%                2 - actual/360
%                3 - actual/365
%                4 - 30/360     PSA
%                5 - 30/360     ISDA
%                6 - 30E/360
%                7 - actual/365 Japanese
%                8 - actual/actual ISMA
%                9 - actual/360 ISMA
%               10 - actual/365 ISMA
%               11 - 30/360 ISMA
%               12 - actual/365 ISDA
%               13 - bus/252
%
%   Outputs:
%       INT - Scalar or vector of accrued interest.
%
%   Example:
%      int = acrudisc('05/01/1992','07/15/1992',100, 0.1, 2, 0)
%
%      int = 2.0604
%
%   See also ACRUBOND, YLDDISC, PRDISC, YLDMAT, PRMAT.

%       Copyright 1995-2006 The MathWorks, Inc.
%       $Revision: 1.6.2.9 $   $Date: 2009/05/07 18:23:15 $

%   Reference: SIA Standard Securities Calculation Methods,
%              Volumes I-II, 3rd Edition.   Formula D

% Input Validation
if nargin < 4
    error('Finance:acrudisc:missingInputs', 'Too few inputs.')
end

if nargin < 5 || isempty(per)
    per = 2*ones(size(rv));
end

if nargin < 6 || isempty(basis)
    basis = zeros(size(rv));
end

if any(ischar(sd) || ischar(md) || iscell(sd) || iscell(md))
    sd = datenum(sd);
    md = datenum(md);
end

% Check to see if the inputs are rows -- this is legacy functionality so it
% needs to be preserved
[~, nCols] = size(sd);
if nCols > 1
    isHorz = true;
else
    isHorz = false;
end

[~, sd, md, ~, basis, ~, ~, ...
    ~, ~, ~, ~] = ...
    instargbond([],sd(:),md(:),[],basis(:),[],[],[],[],[],rv(:));

[sd, md, rv, disc, per, basis] = ...
    finargsz(1,sd(:), md(:), rv(:), disc(:), per(:), basis(:));

int = daysdif(sd,md,basis)./cpnpersz(sd,md,per,basis).*disc./per.*rv;

ismaidx = isisma(basis);

if ~isempty(ismaidx)
    int(ismaidx) = yearfrac(sd(ismaidx),md(ismaidx),...
        basis(ismaidx)).*disc(ismaidx)./per(ismaidx).*rv(ismaidx);% Formula D
end

if isHorz
    int = int';
end